package com.atguigu.yygh.cmn.controller;

import com.atguigu.yygh.cmn.service.CmnService;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author ybl
 * @version 1.9
 * @date 2021/5/14 14:51
 */
@Api(tags = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
public class CmnController {

  @Autowired
  private CmnService cmnService;

  @ApiOperation("根据dictCode获取下级节点")
  @GetMapping("/findByDictCode/{dictCode}")
  public Result<List<Dict>> findByDictCode(@PathVariable("dictCode") String dictCode){
    List<Dict> list = cmnService.findByDictCode(dictCode);
    return Result.ok(list);
  }

  // 根据dictcode和value查询字典名称
  @ApiOperation("根据dictcode和value查询字典名称")
  @GetMapping("getName/{dictCode}/{value}")
  public String getName(
      @ApiParam(name = "dictCode",value = "字典编码",required = true)
      @PathVariable String dictCode,
      @ApiParam(name = "value",value = "值",required = true)
      @PathVariable String value){
    String dictName = cmnService.getNameByParentDictCodeAndValue(dictCode,value);
    return dictName;
  }

  // 根据value查询字典名称
  @ApiOperation(value = "根据value查询字典名称")
  @GetMapping("getName/{value}")
  public String getName(
      @ApiParam(name = "value",value = "值",required = true)
      @PathVariable String value){

    String dictName = cmnService.getNameByParentDictCodeAndValue(null,value);
    return dictName;
  }

  // 数据字典导入
  @ApiOperation(value = "数据字典导入")
  @PostMapping("importDictData")
  public void importData(MultipartFile file){
    cmnService.importDictData(file);
  }

  // 数据字典导出
  @ApiOperation(value = "数据字典导出")
  @GetMapping("exportData")
  public void exportDict(HttpServletResponse response){
    cmnService.exportDictData(response);
  }

  // 查找该id下的子节点
  @ApiOperation(value = "根据id查找该id下的子节点")
  @GetMapping("findChildData/{id}")
  public Result findChildren(@PathVariable Long id){
    if (id != null){
      List<Dict> childData = cmnService.findChlidData(id);
      return Result.ok(childData);
    }
    return Result.fail();
  }


}
